﻿namespace UnityEditor
{
    using Microsoft.Win32;
    using System;
    using System.IO;
    using UnityEngine;

    public class AndroidSdkRoot
    {
        internal static string Browse(string sdkPath)
        {
            string defaultName = (Application.platform != RuntimePlatform.OSXEditor) ? "android-sdk-windows" : "android-sdk-mac_x86";
            string title = "Select Android SDK root folder";
            string folder = !string.IsNullOrEmpty(sdkPath) ? sdkPath : GuessPerPlatform();
            do
            {
                sdkPath = EditorUtility.OpenFolderPanel(title, folder, defaultName);
                if (sdkPath.Length == 0)
                {
                    return string.Empty;
                }
            }
            while (!IsSdkDir(sdkPath));
            return sdkPath;
        }

        private static string GuessPerPlatform()
        {
            RuntimePlatform platform = Application.platform;
            if (platform != RuntimePlatform.OSXEditor)
            {
                if (platform != RuntimePlatform.WindowsEditor)
                {
                    return string.Empty;
                }
                string str = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Android SDK Tools", "Path", string.Empty).ToString();
                string str2 = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Android SDK Tools", "Path", string.Empty).ToString();
                string environmentVariable = Environment.GetEnvironmentVariable("ProgramFiles");
                string str4 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
                if (!string.IsNullOrEmpty(str))
                {
                    return str;
                }
                if (!string.IsNullOrEmpty(str2))
                {
                    return str2;
                }
                if (!string.IsNullOrEmpty(str4))
                {
                    return str4;
                }
                if (!string.IsNullOrEmpty(environmentVariable))
                {
                    return environmentVariable;
                }
            }
            else
            {
                string str5 = Environment.GetEnvironmentVariable("HOME");
                if (!string.IsNullOrEmpty(str5))
                {
                    return str5;
                }
            }
            return string.Empty;
        }

        internal static bool IsSdkDir(string path)
        {
            return Directory.Exists(Path.Combine(path, "tools"));
        }
    }
}

